package cn.ware.house.interfaces.controller;

import cn.ware.house.interfaces.entity.Inventory;
import cn.ware.house.interfaces.service.InventoryService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 库存信息相关的控制器类，负责接收前端的HTTP请求，
 * 调用服务层（InventoryService）的方法来处理业务逻辑，
 * 并将处理结果以合适的格式返回给前端，实现前后端的数据交互和业务流程的驱动。
 */
@RestController
@RequestMapping("/inventories")
public class InventoryController {

    private final InventoryService inventoryService;

    public InventoryController(InventoryService inventoryService) {
        this.inventoryService = inventoryService;
    }

    // 根据库存记录编号查询库存信息，返回单个库存详情
    @GetMapping("/{inventoryId}")
    public ResponseEntity<Inventory> getInventoryById(@PathVariable Integer inventoryId) {
        Inventory inventory = inventoryService.findInventoryById(inventoryId);
        if (inventory!= null) {
            return ResponseEntity.ok(inventory);
        }
        return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
    }

    // 查询所有库存信息，返回库存列表
    @GetMapping
    public ResponseEntity<List<Inventory>> getInventories() {
        List<Inventory> inventories = inventoryService.findAllInventories();
        return ResponseEntity.ok(inventories);
    }

    // 插入库存信息
    @PostMapping
    public ResponseEntity<Void> addInventory(@RequestBody Inventory inventory) {
        boolean success = inventoryService.insertInventory(inventory);
        if (success) {
            return ResponseEntity.status(HttpStatus.CREATED).build();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    // 根据库存记录编号更新库存信息
    @PutMapping("/{inventoryId}")
    public ResponseEntity<Void> updateInventory(@PathVariable Integer inventoryId, @RequestBody Inventory inventory) {
        inventory.setInventoryId(inventoryId);
        boolean success = inventoryService.updateInventoryById(inventory);
        if (success) {
            return ResponseEntity.ok().build();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    // 根据库存记录编号删除库存信息
    @DeleteMapping("/{inventoryId}")
    public ResponseEntity<Void> deleteInventory(@PathVariable Integer inventoryId) {
        boolean success = inventoryService.deleteInventoryById(inventoryId);
        if (success) {
            return ResponseEntity.ok().build();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
